//
//  ChatViewController.m
//  EasyChat
//
//  Created by dllo on 16/6/13.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ChatViewController.h"
#import "ReceiveTableViewCell.h"

@interface ChatViewController ()<UITableViewDataSource, UITableViewDelegate,EMChatManagerDelegate,UIAlertViewDelegate>
//@property (nonatomic, retain) UITableView *tableView;

@property(nonatomic,copy)NSString *buddyUsername;//好友名字
@property(nonatomic,retain)UITableView *myTableView;


@end

@implementation ChatViewController

- (void)dealloc
{
 [[EaseMob sharedInstance].chatManager removeDelegate:self];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
    //修改导航栏标题的颜色 和 大小
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:19],
                                                                      
                                                                      NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    self.title = @"对话";

    //设置代理
    [[EaseMob sharedInstance].chatManager addDelegate:self delegateQueue:nil];
    


}
#pragma mark - chatmanager 代理方法
 //1.监听网络状态
-(void)didConnectionStateChanged:(EMConnectionState)connectionState{

//    eEMConnectionConnected,   //连接成功
//    eEMConnectionDisconnected,//未连接
    
    if (connectionState == eEMConnectionDisconnected) {//网络断开
        NSLog(@"网络断开 未连接...");
        self.title = @"未连接";
    }else{
    
        NSLog(@"网络通了 已连接...");
    }


}

#pragma mark - 代理方法
   //2.监听自动连接的状态
-(void)willAutoReconnect{

    NSLog(@"将自动重新连接");
}

-(void)didAutoReconnectFinishedWithError:(NSError *)error{

    if (!error) {
        NSLog(@"自动重新连接成功");
        self.title = @"对话";
    }else{
    
        NSLog(@"自动重新连接失败,%@",error);
    }

}

#pragma mark - 接受到好友的请求通知
-(void)didReceiveBuddyRequest:(NSString *)username message:(NSString *)message{
    
    //赋值
    self.buddyUsername = username;
    
    //对话框
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"好友添加请求" message:message delegate:self cancelButtonTitle:@"拒绝" otherButtonTitles:@"同意", nil];
    [alert show];
    
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) { //拒绝好友请求
        
        
        [[EaseMob sharedInstance].chatManager rejectBuddyRequest:self.buddyUsername reason:@"我才不要加你" error:nil];
    }else{//同意好友请求
    
        [[EaseMob sharedInstance].chatManager acceptBuddyRequest:self.buddyUsername error:nil];
    
    }


}

#pragma mark - 监听好友被删除

-(void)didRemovedByBuddy:(NSString *)username{

    NSString *message = [username stringByAppendingString:@" 把你删除"];
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"好友移除消息" message:message delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
    [alert show];

}



//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
//{
//    return 10;
//}
//
//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
//    
//    
//    
//    
//    return cell;
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
